package view;
import java.awt.*;
import java.awt.image.*;

import model.*;

/**
 * Screen class. Handles all rendering.
 * Extends canvas, this is where the drawing happens.
 * @author Tim Cocu
 */
public class Screen extends Canvas
{
	private static final long serialVersionUID = -4564078099111469388L;
	
	private int iWidth, iHeight;
	private Model model;
	
	private String latestMessage;
	
	/**
	 * Constructor for instances of the screen class.
	 * @param model The model the view should get objects from.
	 * @param iWidth The width of the screen.
	 * @param iHeight The height of the screen.
	 */
	public Screen(Model model, int iWidth, int iHeight)
	{
		this.iWidth = iWidth;
		this.iHeight = iHeight;
		this.model = model;
	}
	
	/**
	 * Method for rendering onto the Canvas.
	 * @param iFramesLastSecond The amount of frames that have been rendered last second.
	 * @param iUpdatesLastSecond The amount of updates that have been thrown last second.
	 * @param dMouseY The current mouse X.
	 * @param dMouseX The current mouse Y.
	 */
	public void render(int iFramesLastSecond, int iUpdatesLastSecond)
	{
		//ALL PREPARE BUFFERSTRATEGY
		BufferStrategy bufferStrategy = this.getBufferStrategy();
		
		if(bufferStrategy == null)
		{
			this.createBufferStrategy(3);
			return;
		}

		//PREPARE GRAPHIC2D OBJECT
		Graphics2D g2d = (Graphics2D) bufferStrategy.getDrawGraphics();
		
		//DRAW A BACKGROUND ON WHICH WILL BE RENDERED
		g2d.fillRect(0, 0, iWidth, iHeight);
		
		//ALL SETTINGS HANDLING BELOW THIS POINT
		if(Settings.AntiAliasing)
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);	
		if(Settings.HighQuality)
			g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		
		//ALL TRUE RENDERING BELOW THIS POINT.
		g2d.translate(model.getTranslateX(), model.getTranslateY());
		model.getWorld().render(g2d);
		

		
		//ALL DEBUGGING BELOW THIS POINT
		//TRANSLATE BACK SO THIS OVERLAYS
		//MAYBE TRANSLATE BACK IN DEBUG METHOD?
		g2d.translate(-model.getTranslateX(), -model.getTranslateY());
		
		if(Settings.IsTyping)
		drawTextBox(g2d);
		
		if(Settings.DebugMode)
			drawDebug(g2d, iFramesLastSecond, iUpdatesLastSecond);

		//DISPOSING & SHOWING
		g2d.dispose();
		bufferStrategy.show();
	}
	
	public void addCharacter(char character)
	{
		latestMessage += character;
	}
	
	private void drawTextBox(Graphics2D g2d)
	{
		int iChatHeight = 200;
		
		g2d.setColor(new Color(150, 150, 150, 200));
		g2d.fillRect(0, iHeight - iChatHeight, iWidth, iChatHeight - 20);
		g2d.setColor(Color.WHITE);
		g2d.drawLine(0, iHeight - iChatHeight, iWidth, iHeight - iChatHeight);
		
		int messageIndex = 0;
		
		if(model.getChatHistory().size() > 0)
		{
			for(int iIndex = model.getChatHistory().size() - 1; iIndex > (model.getChatHistory().size() - 11); iIndex--)
			{
				if(model.getChatHistory().size() >= iIndex && iIndex >= 0)
				{	
					g2d.drawString(model.getChatHistory().get(iIndex), 10 , (iHeight - 50) - (messageIndex * 14));
				}
					messageIndex++;
			}
		}
		
		g2d.setColor(Color.RED);
		g2d.drawString(model.getLatestMessage(), 10, iHeight - 35);
	}
	
	/**
	 * Method for drawing the debugscreen.
	 * @param g2d Graphics2D object the debug should be drawn onto.
	 * @param iFramesLastSecond The amount of frames that have been rendered last second.
	 * @param iUpdatesLastSecond The amount of updates that have been rendered last second.
	 * @param dMouseX The X position of the mouse.
	 * @param dMouseY The Y position of the mouse.
	 */
	private void drawDebug(Graphics2D g2d, int iFramesLastSecond, int iUpdatesLastSecond)
	{
		double dMouseX = model.getLatestMousePos().getX();
		double dMouseY = model.getLatestMousePos().getY();

		g2d.setStroke(new BasicStroke(2.0f));
		g2d.setColor(new Color(0x0A67A4));
		g2d.fillRect(-5, -5, 130, 120);
		g2d.setColor(Color.WHITE);
		g2d.drawRect(-5, -5, 130, 120);
		
		g2d.setColor(Color.WHITE);
		g2d.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
		g2d.drawString("UPS: " + iUpdatesLastSecond, 5, 14);
		g2d.drawString("FPS: " + iFramesLastSecond, 5, 28);
		g2d.drawString("MX: " + dMouseX, 5, 42);
		g2d.drawString("MY: " + dMouseY, 5, 56);
		g2d.drawString("MX+T: " + (dMouseX - model.getTranslateX()) , 5, 68);
		g2d.drawString("MY+T: " + (dMouseY - model.getTranslateY()) , 5, 80);
		g2d.drawString("AA: " + Settings.AntiAliasing, 5, 94);
		g2d.drawString("HQ: " + Settings.HighQuality, 5, 108);
	}

	public void notifyEndOfMessage() 
	{
		
	}
}
